package com.yinp.proapp.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;

import androidx.core.app.NotificationCompat;

import com.yinp.proapp.R;
import com.yinp.proapp.constant.SpConstants;

import java.util.Random;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.NotificationMessage;

/**
 * @author :yinpeng
 * date      :2021/8/24
 * package   :com.yinp.proapp.utils
 * describe  :
 */
public class JPushUtils {
    public static void initChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (nm != null) {
                NotificationChannelGroup notificationChannelGroup = new NotificationChannelGroup("MyGroupId", "自定义通知组");
                nm.createNotificationChannelGroup(notificationChannelGroup);

                NotificationChannel notificationChannel = new NotificationChannel("jiguangId", "极光推送", NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setGroup("MyGroupId");
                notificationChannel.enableLights(true);
                notificationChannel.enableVibration(true);
//                notificationChannel.setSound("android.resource://com.yinp.proapp/raw/铃声文件", null);    //设置自定义铃声


                nm.createNotificationChannel(notificationChannel);
                SpUtils.saveValue(true, SpConstants.IS_CREATE_NOTIFICATION_CHANNEL);
            }
        }
    }

    /**
     * 设置极光推送的别名
     */
    public static void setAlias(Context context) {
        if (!TextUtils.isEmpty(SpUtils.getValue(SpConstants.JIGUANG_ALIAS, ""))) {
            return;
        }
//        延迟10秒设置别名，不延迟几乎不成功
        new Handler().postDelayed(() -> {
            //第二个参数 用户自定义的操作序列号，同操作结果一起返回，用来标识一次操作的唯一性，推荐每次都用不同的数字序号。
            Random random = new Random();
            long time = System.currentTimeMillis();
            SpUtils.saveValue(String.valueOf(time), SpConstants.JIGUANG_ALIAS);
//                JPushInterface.setAlias(context, random.nextInt(), String.valueOf(time));
            JPushInterface.setAlias(context, random.nextInt(), "22224444");
        }, 10000);
    }

    public static void setNotificationStyle(Context context) {
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(context);
        builder.statusBarDrawable = R.mipmap.add_photos;
        builder.notificationFlags = Notification.FLAG_AUTO_CANCEL
                | Notification.FLAG_SHOW_LIGHTS;  //设置为自动消失和呼吸灯闪烁
//        builder.notificationDefaults = Notification.DEFAULT_SOUND
//                | Notification.DEFAULT_VIBRATE
//                | Notification.DEFAULT_LIGHTS
//                | Notification.PRIORITY_HIGH;  // 设置为铃声、震动、呼吸灯闪烁都要
//        NotificationMessage notificationMessage = new NotificationMessage();
//        notificationMessage.notificationChannelId = "jiguangId";
//        notificationMessage.notificationTitle = "asa";
//        notificationMessage.notificationPriority = NotificationCompat.PRIORITY_HIGH;
//        builder.buildNotification(context, notificationMessage);
        JPushInterface.setPushNotificationBuilder(11, builder);


//        BasicPushNotificationBuilder builder2 = new BasicPushNotificationBuilder(context);
//        builder2.statusBarDrawable = R.mipmap.default1;
//        builder2.notificationFlags = Notification.FLAG_AUTO_CANCEL
//                | Notification.FLAG_SHOW_LIGHTS;  //设置为自动消失和呼吸灯闪烁
//        builder2.notificationDefaults = Notification.DEFAULT_SOUND
//                | Notification.DEFAULT_VIBRATE
//                | Notification.DEFAULT_LIGHTS
//                | Notification.FLAG_HIGH_PRIORITY;  // 设置为铃声、震动、呼吸灯闪烁都要
//        JPushInterface.setPushNotificationBuilder(102, builder2);

    }
}
